package org.garden.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.util.Random;

public class RandomUtil {

    public static String number(int length) {
        return get(length, null, 10);
    }

    public static String string(int length) {
        String pool = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        return get(length, pool, pool.length());
    }

    private static String get(int length, String pool, int max) {
        StringBuilder builder = new StringBuilder();
        for (int i=0;i<length;i++) {
            if (StringUtils.isNotBlank(pool)) {
                builder.append(pool.charAt(next(max)));
            } else {
                builder.append(next(max));
            }
        }
        return builder.toString();
    }

    private static int next(Integer max) {
        Random r = new Random();
        if (max != null) {
            return r.nextInt(max);
        }
        return r.nextInt();
    }
}
